package com.hm.base.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hm.base.auto.helper.HmSessionFactory;
import com.hm.base.auto.su.ControllerResult;
import com.hm.base.auto.su.R;
import com.hm.base.auto.su.R.LogConfig;
import com.hm.base.auto.su.R.Restful;
import com.hm.base.service.SystemConfigDictService;
import com.hm.base.vo.SystemConfigDictVo;
import com.hm.common.annotation.WebClientOperationLog;
import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.PageInfo.PageParam;
import com.hm.common.su.bean.ServerResponse;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 上午11:05:32 2017年6月14日
 * @version 1.0
 * @describe 系统配置管理
 */
@Slf4j
@ResponseBody
@RestController
@RequestMapping(Restful.API + "/syscnf")
public class SystemConfigDictApi {

	private static final String SWWAGER_TAG = "系统配置管理";

	@Autowired
	private SystemConfigDictService systemConfigDictService;

	@RequestMapping(value = "/types", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "加载系统数据字典所有分类", httpMethod = "GET", notes = "com.hm.base.api.SystemConfigDictApi::loadSystemConfigDictTypes")
	public ResponseEntity<ServerResponse<List<SystemConfigDictVo>>> loadSystemConfigDictTypes() {
		log.debug("用户{},加载系统数据字典所有分类", HmSessionFactory.currentUserId());
		return ControllerResult.success(systemConfigDictService.loadSystemConfigDictByDictType(R.DicConfig.SYSTEM_DICT_TYPE));
	}

	@RequestMapping(value = "/dict/{code}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "通过CODE加载系统数据字典", httpMethod = "GET", notes = "com.hm.base.api.SystemConfigDictApi::loadSystemConfigDictByDictCode")
	public ResponseEntity<ServerResponse<SystemConfigDictVo>> loadSystemConfigDictByDictCode(@ApiParam(required = true, name = "code", value = "字典CODE") @PathVariable("code") String code) {
		log.debug("用户{},加载系统数据字典", HmSessionFactory.currentUserId());
		return ControllerResult.success(systemConfigDictService.loadSystemConfigDictByDictCode(code));
	}

	@RequestMapping(value = "/dicts/{dictType}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "通过字典分类加载系统数据字典列表", httpMethod = "GET", notes = "com.hm.base.api.SystemConfigDictApi::loadSystemConfigDictByDictType")
	public ResponseEntity<ServerResponse<List<SystemConfigDictVo>>> loadSystemConfigDictByDictType(@ApiParam(required = true, name = "dictType", value = "字典分类") @PathVariable("dictType") String dictType) {
		log.debug("用户{},加载系统数据字典列表", HmSessionFactory.currentUserId());
		return ControllerResult.success(systemConfigDictService.loadSystemConfigDictByDictType(dictType));
	}

	@RequestMapping(value = "/dicts/{pageNumber}/{pageSize}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "分页系统数据字典列表", httpMethod = "GET", notes = "com.hm.base.api.SystemConfigDictApi::query")
	public ResponseEntity<ServerResponse<PageInfo<SystemConfigDictVo>>> query(@ApiParam(required = true, name = "pageNumber", value = "分页号(第一页值为1,最小页码不能小于1)") @PathVariable("pageNumber") int pageNumber,
			@ApiParam(required = true, name = "pageSize", value = "分页大小") @PathVariable("pageSize") int pageSize, @ApiParam(required = false, name = "dictType", value = "字典分类") @RequestParam(required = false, name = "dictType") String dictType,
			@ApiParam(required = false, name = "code", value = "字典CODE") @RequestParam(required = false, name = "code") String code) throws Exception {
		log.debug("用户{},查询系统数据字典列表", HmSessionFactory.currentUserId());
		return ControllerResult.success(systemConfigDictService.query(new PageParam(pageNumber, pageSize), dictType, code));
	}

	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation(tags = SWWAGER_TAG, value = "新增数据字典信息", httpMethod = "POST", notes = "com.hm.base.api.SystemConfigDictApi::addSystemConfigDict")
	@WebClientOperationLog(platform=LogConfig.BUS_SYSTEM,module=LogConfig.SYSTEM_CONFIG_DICT_MODULE,describe="新增数据字典信息")
	public ResponseEntity<ServerResponse<Boolean>> addSystemConfigDict(@ApiParam(required = true, name = "body") @RequestBody(required = true) SystemConfigDictVo systemConfigDictVo) {
		log.debug("用户{}新增数据字典信息", HmSessionFactory.currentUserId());
		systemConfigDictVo.setCreateUser(HmSessionFactory.currentUserId());
		systemConfigDictService.addSystemConfigDict(systemConfigDictVo);
		return ControllerResult.success(true);
	}

	@RequestMapping(value = "/{dictId}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "获取数据字典信息", httpMethod = "GET", notes = "com.hm.base.api.SystemConfigDictApi::getSystemConfigDict")
	public ResponseEntity<ServerResponse<SystemConfigDictVo>> getSystemConfigDict(@ApiParam(required = true, name = "dictId", value = "数据字典ID") @PathVariable("dictId") Long dictId) {
		log.debug("用户{}获取数据字典信息", HmSessionFactory.currentUserId());
		return ControllerResult.success(systemConfigDictService.getSystemConfigDict(dictId));
	}

	@RequestMapping(value = "/{dictId}", method = RequestMethod.PUT)
	@ApiOperation(tags = SWWAGER_TAG, value = "修改数据字典信息", httpMethod = "PUT", notes = "com.hm.base.api.SystemConfigDictApi::updateSystemConfigDict")
	@WebClientOperationLog(platform=LogConfig.BUS_SYSTEM,module=LogConfig.SYSTEM_CONFIG_DICT_MODULE,describe="修改[#{#dictId}]数据字典信息")
	public ResponseEntity<ServerResponse<Boolean>> updateSystemConfigDict(@ApiParam(required = true, name = "dictId", value = "数据字典ID") @PathVariable("dictId") Long dictId, @ApiParam(required = true, name = "body") @RequestBody(required = true) SystemConfigDictVo systemConfigDictVo) {
		log.debug("用户{}修改数据字典信息", HmSessionFactory.currentUserId());
		systemConfigDictVo.setId(dictId);
		systemConfigDictService.updateSystemConfigDict(systemConfigDictVo);
		return ControllerResult.success(true);
	}

	@RequestMapping(method = RequestMethod.DELETE)
	@ApiOperation(tags = SWWAGER_TAG, value = "删除数据字典信息", httpMethod = "DELETE", notes = "com.hm.base.api.SystemConfigDictApi::deleteSystemConfigDict")
	@WebClientOperationLog(platform=LogConfig.BUS_SYSTEM,module=LogConfig.SYSTEM_CONFIG_DICT_MODULE,describe="删除[#{#{dictIds}}]数据字典信息")
	public ResponseEntity<ServerResponse<Boolean>> deleteSystemConfigDict(@ApiParam(required = true, name = "dictIds", value = "数据字典ID") @RequestParam(required = true, name = "dictIds") List<Long> dictIds) {
		log.debug("用户{}删除数据字典信息", HmSessionFactory.currentUserId());
		systemConfigDictService.deleteSystemConfigDict(dictIds);
		return ControllerResult.success(true);
	}
}
